﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace TestLogMonitor.Common
{
    public class ViewManager
    {
        public static Dictionary<string, ViewManagerExtension> dicWin = new Dictionary<string, ViewManagerExtension>();
        public static Dictionary<string, List<Window>> dicShowWin = new Dictionary<string, List<Window>>(); //记录显示的窗口
        /// <summary>
        /// 视图注册
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="type">当前要注册视图Type类型</param>
        /// <param name="mainWindow">主视图《没有可以填null》</param>
        public static void Register(string key, Type type, Window mainWindow)
        {
            if (!dicWin.Keys.Contains(key))
            {
                dicWin.Add(key, new ViewManagerExtension() { SubType = type, MainWindow = mainWindow });
            }
        }
        /// <summary>
        /// 显示模态化的视图
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static bool ShowDialog(string key)
        {
            if (dicWin[key] != null)
            {
                var subWindow = ((Window)Activator.CreateInstance(dicWin[key].SubType));

                subWindow.Owner = dicWin[key].MainWindow;
                if (!dicShowWin.ContainsKey(key))
                {
                    dicShowWin[key] = new List<Window>() { subWindow };
                }
                else
                {
                    var showWins = dicShowWin[key];
                    showWins.Add(subWindow);
                    dicShowWin.Remove(key);
                    dicShowWin.Add(key, showWins);
                }
                return subWindow.ShowDialog() == null ? false : true;
            }
            return false;
        }
        /// <summary>
        /// 显示非模态化视图
        /// </summary>
        /// <param name="key">键值</param>
        public static void Show(string key)
        {
            if (dicWin[key] != null)
            {
                var subWin = (Window)Activator.CreateInstance(dicWin[key].SubType);
                subWin.Owner = dicWin[key].MainWindow;
                if (!dicShowWin.ContainsKey(key))
                {
                    dicShowWin[key] = new List<Window>() { subWin };
                }
                else
                {
                    var showWins = dicShowWin[key];
                    showWins.Add(subWin);
                    dicShowWin.Remove(key);
                    dicShowWin.Add(key, showWins);
                }
                subWin.Show();
            }
        }
        /// <summary>
        /// 关闭视图
        /// </summary>
        /// <param name="key">键值</param>
        public static void Close(string key)
        {
            if (dicShowWin[key] != null)
            {
                List<Window> lstClosingWins = dicShowWin[key];
                foreach (var w in lstClosingWins)
                {
                    w.Close();
                }
                dicShowWin.Remove(key);
            }
        }

    }
    public class ViewManagerExtension
    {
        private Type subtype;

        public Type SubType
        {
            get { return subtype; }
            set { subtype = value; }
        }
        private Window mainwindow;

        public Window MainWindow
        {
            get { return mainwindow; }
            set { mainwindow = value; }
        }
    }
}
